package logic

import (
	define2 "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRandomArticleListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRandomArticleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRandomArticleListLogic {
	return &GetRandomArticleListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRandomArticleListLogic) GetRandomArticleList(in *Article.ArticleRequest) (*Article.ArticleVoListResponse, error) {
	var articleList []models.ArticleVo
	models.Db.Model(new(models.ArticleVo)).
		Where("status = ?", define2.PUBLIC_ARTICLE).
		Order("rand()").
		Limit(define2.RANDOM_ARTICLE_COUNT).
		Find(&articleList)
	var articleVoList []*Article.ArticleVo
	copier.Copy(&articleVoList, &articleList)
	for i, v := range articleList {
		articleVoList[i].CreateTime = helper.MyTimeToTimestamp(v.CreateTime)
		articleVoList[i].UpdateTime = helper.MyTimeToTimestamp(v.UpdateTime)
	}
	return &Article.ArticleVoListResponse{
		ArticleVoList: articleVoList,
	}, nil
}
